<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/22
 * Time: 23:48
 */

namespace zch\firstLaravel\facades;

use zch\firstLaravel\application\Application;

abstract class Facade
{
    protected static $resolvedInstance = [];

    //子类必须重写这个方法
    public static function getFacadeClass()
    {
        throw new \Exception('你没有指定代理的门面类', 1);
    }

    /**
     * 获取要调用的这个类
     * @return mixed|void
     * @throws \Exception
     */
    public static function createFacade()
    {
        $class = static::getFacadeClass();
        if(is_object($class)){
            return $class;
        }

        if(isset(static::$resolvedInstance[$class])){
            return static::$resolvedInstance[$class];
        }

        return static::$resolvedInstance[$class] = Application::getInstace()->make($class);

    }

    /**
     * 执行方法
     * @param $method
     * @param $args
     * @return mixed
     * @throws \Exception
     */
    public static function __callStatic($method, $args)
    {
        $class = static::createFacade();
        if(!$class){
            throw new \Exception('类没有找到'.$class, 1);
        }
        return $class->{$method}(...$args);
    }
    
}